package com.eleadmin.store.service.impl;

import com.eleadmin.damon.utils.results.PageResult2;
import com.eleadmin.common.core.web.JsonResult;
import com.eleadmin.common.system.entity.User;
//import com.eleadmin.store.mapper.EntryBillMapper;
import com.eleadmin.damon.utils.utils.ArithUtil;
import com.eleadmin.store.biz.EntryBillSonBiz;
import com.eleadmin.store.biz.ProductStorageBiz;
import com.eleadmin.store.entity.EntryBill;
import com.eleadmin.store.entity.EntryBillSon;
import com.eleadmin.store.entity.ProductStorage;
import com.eleadmin.store.result.PriceResult;
import com.eleadmin.store.service.EntryBillService;
import org.springframework.stereotype.Service;
import com.eleadmin.store.biz.EntryBillBiz;
import net.sf.json.JSONObject;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
//import javax.transaction.Transactional;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 入库单服务实现类
 *
 * @author : Doman
 * @version ：1.0
 * @date : 2024-02-14 14:59:01
 */
@Service
public class EntryBillServiceImpl /*extends ServiceImpl<EntryBillMapper, EntryBill> */ implements EntryBillService {

    @Autowired
    private EntryBillBiz entryBillBiz;
    @Autowired
    private EntryBillSonBiz entryBillSonBiz;
    @Autowired
    private ProductStorageBiz productStorageBiz;

    @Override
    public JsonResult list(PageResult2<T> page, EntryBill entryBill, User loginUser) throws Exception {
        JsonResult jr = entryBillBiz.list(page, entryBill, loginUser);
        jr.put("priceResult", entryBillBiz.entryBillNums(entryBill, loginUser));
        return jr;
    }

    @Override
    public JsonResult entryBillNums(EntryBill entryBill, User loginUser) {
        return JsonResult.ok().setData(entryBillBiz.entryBillNums(entryBill, loginUser));
    }

    @Override
    public JsonResult list(EntryBill entryBill, User loginUser) {
        return JsonResult.ok().setData(entryBillBiz.list(entryBill, loginUser));
    }

    @Override
    public JsonResult getById(String id) {
        return JsonResult.ok().setData(entryBillBiz.getById(id));
    }

    @Transactional
    @Override
    public JsonResult save(EntryBill entryBill, User loginUser) {
        return JsonResult.ok("添加成功").setData(entryBillBiz.save(entryBill, loginUser));
    }

    @Transactional
    @Override
    public JsonResult update(EntryBill entryBill, User loginUser) {
        if (entryBillBiz.update(entryBill, loginUser)) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    @Transactional
    @Override
    public JsonResult updateTableState(EntryBill entryBill, User loginUser) {
        if (entryBillBiz.updateTableState(entryBill, loginUser)) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    @Transactional
    @Override
    public JsonResult remove(String id, User loginUser) {
        if (entryBillBiz.remove(id, loginUser)) {
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }

    @Transactional
    @Override
    public JsonResult removeEntryAndSon(String id, User loginUser) throws Exception {
        String[] ids = id.split(", ");
        //多个入库主表id
        if (ids != null && ids.length > 0) {
            for (int j = 0; j < ids.length; j++) {
                EntryBillSon entryBillSon = new EntryBillSon();
                entryBillSon.setEntryBillId(Integer.valueOf(ids[j]));
                //根据主表id获取子表所有记录
                List<EntryBillSon> list = entryBillSonBiz.list(entryBillSon, loginUser);
                EntryBillSon entryBillSon2 = null;
                if (list != null && list.size() > 0) {
                    for (int i = 0; i < list.size(); i++) {
                        entryBillSon2 = list.get(i);

                        //修改库存
                        ProductStorage ps = new ProductStorage();
                        ps.setWarehousename(entryBillSon2.getWarehousename());
                        ps.setProductname(entryBillSon2.getProductname());
                        ps.setSpecification(entryBillSon2.getSpecification());
                        ps.setLengths(entryBillSon2.getLengths() + "");
                        ps.setNegative(entryBillSon2.getNegative() + "");
                        ProductStorage byName = productStorageBiz.getByName(ps, loginUser);
                        try {
                            byName.setNum(ArithUtil.sub(0, entryBillSon2.getNum()));
                            byName.setLitotalweight(ArithUtil.sub(0,
                                    entryBillSon2.getLitotalweight()));
                            productStorageBiz.updateNum(byName, loginUser);
                        } catch (Exception e) {
                            throw new Exception("没有对应的库存数据！");
                        }
                        //删除子表
                        entryBillSonBiz.remove(entryBillSon2.getId(), loginUser);
                    }
                }
                //主表 总金额更新
                Double total = entryBillSonBiz.calcTotalPrice(entryBillSon, loginUser);
                EntryBill entryBill = new EntryBill(entryBillSon.getEntryBillId() + "", total + "");
                entryBillBiz.updateTotal(entryBill, loginUser);
                //删除主表
                entryBillBiz.remove(id, loginUser);
            }
        }

        return JsonResult.ok("删除成功");
    }

}
